package org.demon.springtest.design.singleton;

import java.io.Serializable;

/**
 * @author demon
 * @version 1.0.0
 */
public class StaticSingleton implements Serializable {

    private StaticSingleton() {
        System.out.println("StaticSingleton is create");
    }

    private static class SingletonHolder {
        private static StaticSingleton instance = new StaticSingleton();
    }

    public static StaticSingleton getInstance() {
        return SingletonHolder.instance;
    }


    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> System.out.println(StaticSingleton.getInstance())).start();
        }
    }

    //处理反序列化漏洞，直接返回已有对象
    private Object readResolve() {
        return SingletonHolder.instance;
    }
}
